package com.xmut.web;

import com.sirdc.modules.core.web.JsonBaseController;
import com.sirdc.modules.core.web.model.JqGrid;
import com.sirdc.modules.core.web.model.Message;
import com.xmut.entity.Repair;
import com.xmut.filter.RepairFilter;
import com.xmut.service.RepairService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 *
 */
@Controller
@RequestMapping("/modules/xmut/repair")
public class RepairController extends JsonBaseController {

    @Autowired
    private RepairService repairService;

    @Override
    protected String getView(String view) {
        return String.format("/modules/xmut/repair/%s",view);
    }

    /**
     * 初始化界面
     *
     * @return url
     */
    @RequiresPermissions("modules:xmut:repair:query")
    @RequestMapping(method = RequestMethod.GET)
    public String initPage() {
        return getView("query");
    }

    /**
     * 输出列表
     *
     * @param filter
     * @return json
     */
    @ResponseBody
    @RequiresPermissions("modules:xmut:repair:query")
    @RequestMapping(method = RequestMethod.POST)
    public JqGrid query(@ModelAttribute RepairFilter filter) {
        List<Repair> repairs = repairService.query(filter);
        return coverJqGrid(filter, repairs);
    }

    /**
     * 跳转至filter界面
     * @param filter
     * @return
     */
    @RequestMapping("/filter")
    @RequiresPermissions("modules:xmut:repair:query")
    public String filter(@ModelAttribute RepairFilter filter) {
        return getView("filter");
    }

    /**
     * 执行创建
     * @param repair
     * @return
     */
    @ResponseBody
    @RequiresPermissions("modules:xmut:repair:create")
    @RequestMapping(value = "/create", method = RequestMethod.POST)
    public Message create(@ModelAttribute Repair repair) {
        repairService.save(repair);
        return coverMessage("200");
    }

    /**
     * 初始化创建界面
     * @param model
     * @return url
     */
    @RequiresPermissions("modules:xmut:repair:create")
    @RequestMapping(value = "/create", method = RequestMethod.GET)
    public String showAddView(@ModelAttribute  Repair repair, Model model) {
        return getView("form");
    }

    /**
     * 初始化更新界面
     *
     * @param model
     * @param id
     * @return url
     */
    @RequiresPermissions("modules:xmut:repair:edit")
    @RequestMapping(value = "/edit/{id}", method = RequestMethod.GET)
    public String showEditView(Model model, @PathVariable Long id) {
        Repair repair = repairService.getById(id);
        model.addAttribute("repair", repair);
        return getView("form");
    }

    /**
     * 初始化更新界面
     *
     * @param model
     * @param id
     * @return url
     */
    @RequiresPermissions("modules:xmut:repair:edit")
    @RequestMapping(value = "/edit2/{id}", method = RequestMethod.GET)
    public String showEditView2(Model model, @PathVariable Long id) {
        Repair repair = repairService.getById(id);
        model.addAttribute("repair", repair);
        return getView("form2");
    }

    /**
     * 更新操作
     * @param repair
     * @return
     */
    @ResponseBody
    @RequiresPermissions("modules:xmut:repair:edit")
    @RequestMapping(value = "/edit", method = RequestMethod.POST)
    public Message update(@ModelAttribute Repair repair) {

        Repair repairDB = repairService.getById(repair.getSysId());
        repairDB.setReplyPeople(repair.getReplyPeople());
        repairDB.setReplyTel(repair.getReplyTel());
        repairDB.setReplyState("维修中");
        repairService.update(repairDB);

        return coverMessage("200");
    }

    /**
     * 更新操作
     * @param repair
     * @return
     */
    @ResponseBody
    @RequiresPermissions("modules:xmut:repair:edit")
    @RequestMapping(value = "/edit2", method = RequestMethod.POST)
    public Message update2(@ModelAttribute Repair repair) {
        Repair repairDB = repairService.getById(repair.getSysId());
        repairDB.setDemo(repair.getDemo());
        repairDB.setReplyState("维修完成");
        repairService.update(repairDB);
        return coverMessage("200");
    }


    /**
     * 删除操作
     * @param id
     * @return
     */
    @ResponseBody
    @RequiresPermissions("modules:xmut:repair:delete")
    @RequestMapping(value = "/delete/{id}")
    public Message delete(@PathVariable List<Long> id) {
        repairService.batchDelete(id);
        return coverMessage("200");
    }
}
